//package com.youlv.yl.filter;
//
//import com.youlv.yl.config.RedisKeyConfig;
//import com.youlv.yl.config.SystemConfig;
//import com.youlv.yl.util.JedisUtils;
//import com.youlv.yl.util.StringUtils;
//import org.codehaus.jettison.json.JSONObject;
//import org.springframework.http.HttpMethod;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//
///**
// * @author fenghe
// * @date 2022/1/10 17:27
// */
//@Component
//@WebFilter("*.do**")
//public class TokenTimeFilter implements Filter {
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        synchronized (ServletRequest.class) {
//
//            // 实现令牌的自动延期，如果当前请求携带的有令牌，那么就自动续命30分钟
//            HttpServletRequest request = (HttpServletRequest) servletRequest;
//            HttpServletResponse response = (HttpServletResponse) servletResponse;
//
//            //输出 OPTIONS/GET/POST。。。
//            String method = request.getMethod();
//            //如果是 OPTIONS 请求，让 OPTIONS 请求返回一个200状态码
//            if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
//                response.setStatus(HttpServletResponse.SC_OK);
//                //放行
//                filterChain.doFilter(servletRequest, servletResponse);
//            } else {
//                String uri = request.getRequestURI();
//                System.out.println(uri);
//                // 获取令牌
//                String token = request.getHeader(SystemConfig.REQ_HEADER_TOKEN);
//                // 校验是否传递令牌
//                if (StringUtils.isNotEmpty(token) && JedisUtils.exists(RedisKeyConfig.USER_TOKEN + token)) {
//                    // 验证令牌是否存在
//                    // 判断传递的令牌是否正确(验证多设备唯一登陆)若不正确，拒绝请求
//                    // 查询在线用户手机号
//                    String phone = JedisUtils.getHash(RedisKeyConfig.USER_UID_PHONE,
//                            JedisUtils.getString(RedisKeyConfig.USER_TOKEN + token));
//                    if(JedisUtils.getString(RedisKeyConfig.USER_PHONE + phone).equals(token)) {
//                        // 存在令牌，则续命
//                        JedisUtils.expire(RedisKeyConfig.USER_TOKEN + token, RedisKeyConfig.USER_TOKEN_TIME);
//
//                        // 手机号的key也需要续命
//                        JedisUtils.expire(RedisKeyConfig.USER_PHONE + phone, RedisKeyConfig.USER_TOKEN_TIME);
//                        //放行
//                        filterChain.doFilter(servletRequest, servletResponse);
//                    } else {
//                        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
//                        response.setHeader("Access-Control-Allow-Credentials", "true");
//                        response.setCharacterEncoding("UTF-8");
//                        response.setContentType("application/json; charset=utf-8");
//                        PrintWriter out = null;
//                        JSONObject res = new JSONObject();
//                        try {
//                            res.put("code", SystemConfig.USER_LOGIN_FLAG);
//                            res.put("msg", "亲，您的账号已在其他设备登陆！！！");
//                            out = response.getWriter();
//                            out.append(res.toString());
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                } else if(uri.startsWith(SystemConfig.FILTER_LOGIN) || uri.startsWith(SystemConfig.FILTER_SMS) || uri.endsWith(SystemConfig.FILTER_SWAGGER_1) || uri.startsWith(SystemConfig.FILTER_SWAGGER_2) || uri.startsWith(SystemConfig.FILTER_SWAGGER_3) || uri.startsWith(SystemConfig.FILTER_SWAGGER_4) || uri.startsWith(SystemConfig.FILTER_SWAGGER_5)) {
//
//                    filterChain.doFilter(servletRequest, servletResponse);
//                } else {
//                    response.setCharacterEncoding("UTF-8");
//                    response.setContentType("application/json; charset=utf-8");
//                    PrintWriter out = null;
//                    JSONObject res = new JSONObject();
//                    try {
//                        res.put("code", 400);
//                        res.put("msg", "亲，请重新登陆！！！");
//                        out = response.getWriter();
//                        out.append(res.toString());
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }
//    }
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//    }
//}
